package com.example.demo;

import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class HelloApplication_ListView extends Application {
    //
    // Set up the model which is two lists of Players and a filter criteria
    //
    ReadOnlyObjectProperty<ObservableList<Dept>> deptsProperty =
            new SimpleObjectProperty<>(FXCollections.observableArrayList(Dept.DEPT_LIST));

    ReadOnlyObjectProperty<FilteredList<Dept>> viewableDeptsProperty =
            new SimpleObjectProperty<FilteredList<Dept>>(
                    new FilteredList<>(deptsProperty.get()
                    ));

    ObjectProperty<Predicate<? super Dept>> filterProperty =
            viewableDeptsProperty.get().predicateProperty();

    @Override
    public void start(Stage primaryStage) throws Exception {

        //
        // Build the UI
        //
        VBox vbox = new VBox();
        vbox.setPadding( new Insets(10));
        vbox.setSpacing(4);

        HBox hbox = new HBox();
        hbox.setSpacing( 2 );

        ToggleGroup filterTG = new ToggleGroup();
        //
        // The toggleHandler action wills set the filter based on the TB selected
        //
        @SuppressWarnings("unchecked")
        EventHandler<ActionEvent> toggleHandler = (event) -> {
            ToggleButton tb = (ToggleButton)event.getSource();
            Predicate<Dept> filter = (Predicate<Dept>)tb.getUserData();
            filterProperty.set( filter );
        };

        ToggleButton tbShowAll = new ToggleButton("Show All");
        tbShowAll.setSelected(true);
        tbShowAll.setToggleGroup( filterTG );
        tbShowAll.setOnAction(toggleHandler);
        tbShowAll.setUserData( (Predicate<Dept>) (Dept p) -> true);

        Button button =new Button("aaaa");
//
        Node node =new Node() {
            @Override
            public boolean hasProperties() {
                return super.hasProperties();
            }
        };


        //
        // Create a distinct list of teams from the Player objects, then create
        // ToggleButtons
        //
        List<ToggleButton> tbs =Dept.DEPT_LIST
                .stream()
                .map( (p) -> p.getLoc() )
                .distinct()
                .map( (team) -> {
                    ToggleButton tb = new ToggleButton( team );
                    tb.setToggleGroup( filterTG );
                    tb.setOnAction( toggleHandler );
                    tb.setUserData( (Predicate<Dept>) (Dept p) -> team.equals(p.getLoc()) );
                    return tb;
                })
                .collect(Collectors.toList());


        hbox.getChildren().addAll( tbShowAll   );
        hbox.getChildren().addAll(tbs);

        ListView<Dept> lv = new ListView<>();
        lv.itemsProperty().bind( viewableDeptsProperty );
        vbox.getChildren().addAll( hbox, lv );


        Scene scene = new Scene(vbox);

        primaryStage.setTitle("CombosApp");
        primaryStage.setScene( scene );
        primaryStage.show();

    }


    public static void main(String[] args) {
        launch();
    }
}